package com.qzimp.fujiancuo.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWT;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.qzimp.fujiancuo.entity.Userinfo;
import com.qzimp.fujiancuo.entity.UserinfoLog;
import com.qzimp.fujiancuo.entity.dto.UserinfoLogDto;
import com.qzimp.fujiancuo.entity.vo.ResultCode;
import com.qzimp.fujiancuo.entity.vo.UserHaveLogsVo;
import com.qzimp.fujiancuo.entity.vo.UserinfoHaveLogsVo;
import com.qzimp.fujiancuo.exceptions.ApiException;
import com.qzimp.fujiancuo.mapper.UserinfoMapper;
import com.qzimp.fujiancuo.service.UserinfoLogService;
import com.qzimp.fujiancuo.service.UserinfoService;
import com.qzimp.fujiancuo.utils.StringUtil;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  服务层实现。
 *
 * @author 吴宗波
 * @since 2024-11-21
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements UserinfoService {
    @Autowired
    private  UserinfoMapper userinfoMapper;
    @Autowired
    private UserinfoLogService userinfoLogService;

    /*修改密码*/
    public void updatePassWord(Integer id,  String newPassword) {
        String password = StringUtil.MD5(newPassword);
            userinfoMapper.updatePassWord(id,password);
    }

    /*根据用户id查询该用户的所有事件信息*/
    public UserinfoLogDto getUserinfoByLogId(Integer id) {
        return userinfoMapper.getUserinfoByLogId(id);
    }

    @Override
    public Userinfo findUserinfoByLoginName(String loginName) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("login_name",loginName);

        return userinfoMapper.selectOneByQuery(queryWrapper);
    }

    @Override
    public Userinfo login(String loginName, String password) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("login_name",loginName);
        queryWrapper.eq("password",password);
        Userinfo userinfo= userinfoMapper.selectOneByQuery(queryWrapper);
        if(userinfo==null){
            throw  new ApiException(ResultCode.USER_LOGIN_NAME_NOT_EXIST);
        }
        //生成token

        final  String token= JWT.create()
                .setNotBefore(DateUtil.date())
                .setKey("abcdef".getBytes())
                .sign();
        userinfo.setToken(token);
        //更新token
        int ok= userinfoMapper.update(userinfo);
        if(ok<1){
            if(userinfo==null){
                throw  new ApiException(ResultCode.USER_TOKEN_UPDATE_FAILURE);
            }
        }

        return  userinfo;

    }

/*通过用户登录成功用户名获取用户日志*/
    public UserinfoHaveLogsVo findUserinfoHaveLogsVo(String loignName) {
        //通过username用户的信息
        Userinfo userinfo=this.findUserinfoByLoginName(loignName);
        if(userinfo==null){
            throw new ApiException(ResultCode.CUSTOM_ERROR,"该用户不存在");

        }
        //通过用户编号获取用户日志
        List<UserinfoLog> logs= userinfoLogService.findUserinfoLogsById(userinfo.getId());
        UserinfoHaveLogsVo userinfoHaveLogsVo=new UserinfoHaveLogsVo();
        userinfoHaveLogsVo.setUserinfoLogList(logs);
        userinfoHaveLogsVo.setLoginName(loignName);
        return userinfoHaveLogsVo;
    }

    @Override
    public UserHaveLogsVo queryUserHaveLogs(String loginName) {
        //获取管理员信息通过管理员账号

        Userinfo userinfo=this.queryUserinfoByName(loginName);

        if(userinfo==null){
            throw  new ApiException(ResultCode.CUSTOM_ERROR,"用户账户账户不存在");
        }

        // 通过管理员编号获取日志信息
        List<UserinfoLog> logList= userinfoLogService.queryUserinfoLogs(userinfo.getId());


        //把所有的信息放到包装类AdminHaveLogsVo
        UserHaveLogsVo userHaveLogsVo=new UserHaveLogsVo();

        userHaveLogsVo.setLoginName(loginName);
        userHaveLogsVo.setUserinfoLogList(logList);

        return userHaveLogsVo;
    }

    private Userinfo queryUserinfoByName(String loginName) {
        QueryWrapper queryWrapper=new QueryWrapper();

        queryWrapper.eq("login_name",loginName);

        return userinfoMapper.selectOneByQuery(queryWrapper);
    }
        public void register(String loginName, String password) {
        // 检查账号是否已存在
        Userinfo existingUser = findUserinfoByLoginName(loginName);
        if (existingUser != null) {
            throw new RuntimeException("账号已存在");
        }

        // 创建新用户
        Userinfo newUser = new Userinfo();
        newUser.setLoginName(loginName);
        newUser.setPassword(StringUtil.MD5(password)); // 密码加密

        // 插入用户信息到数据库
        userinfoMapper.register(newUser);
    }
}
